[CI] Fix BuggyBits port-binding TOCTOU race in profiler integration tests#8705
[CI] Fix BuggyBits port-binding TOCTOU race in profiler integration tests#8705datadog-datadog-prod-us1-2[bot] wants to merge 1 commit into
Conversation
BenchmarksBenchmark execution time: 2026-05-28 16:00:02 Comparing candidate commit 9617f6f in PR branch Found 0 performance improvements and 0 performance regressions! Performance is the same for 72 metrics, 0 unstable metrics, 61 known flaky benchmarks, 65 flaky benchmarks without significant changes.
|
Summary
Fixes a TOCTOU (Time-of-Check-Time-of-Use) race condition in
Samples.BuggyBitsthat causes intermittentSystem.IO.IOException: Failed to bind to address … address already in usefailures in profiler integration tests.Root cause
TestApplicationRunner.BuildTestCommandLinecallsTcpPortProvider.GetOpenPort()to find a free port, then passes it as--urls http://localhost:{port}to the BuggyBits process. Inside BuggyBits,CreateHostBuilder(args)builds the host immediately using those args — Kestrel reads--urlsand binds the port at that point.The existing
GetValidPortcall inProgram.Mainthen probes whether the port is free — but this happens after the host is already built with the original port. The probe result only updatesrootUrlforSelfInvoker; Kestrel never sees the corrected port. The "race avoidance" code was therefore a no-op for the actual bind failure.On a busy CI runner (10+ containers: ElasticMQ, Localstack, Postgres, Kafka, RabbitMQ, MsSQL, MySQL, etc.) the window between
TcpPortProvider's probe and Kestrel's bind is wide enough for another process to grab the port.Fix
Move the URL/port resolution before
CreateHostBuilder(args):ResolveListenUrl: extracts--urlsfrom args, callsFindFreePortUrl(up to 5 retries with fresh ephemeral ports), returns updated args and the resolved URL.FindFreePortUrl: probes port availability, retries with OS-assigned ephemeral ports on failure.IsPortAvailable: atomicHttpListenerprobe (extracted from the oldGetValidPort).ReplaceUrlInArgs: patches the--urlsvalue in args soCreateHostBuilderconfigures Kestrel with the correct port.GetOpenPortis unchanged — it is used by other tests.Fixes recurring CI failures on commits:
1be7d52e,dc3f873a,ba7828b5,fb4364172,169fbf4d.